<template>
	
	<view>
		<cu-custom bgColor="bg-gradual-pink" isBack="false">
			<block slot="content">用户中心</block>		
		</cu-custom>
		<view class="flex userinfo">
			<image class="userinfo-headimg" :src="headimg==''?'/static/tennis/no-login.jpg':headimg"></image>
			<view class="userinfo-det">
				<view v-if="hasLogin" :style="[{height:hasLogin?'50%':'0'}]" class="userinfo-nickname">{{nickname}}</view>
				<button v-if="!hasLogin"  class="userinfo-login cu-btn round bg-white" open-type="getUserInfo" @getuserinfo="wxgetInfo">点击登陆</button>
				<view class="userinfo-detail" @tap="navtoInfo" >个人信息<label class="cuIcon-right"></label></view>
			</view>
		</view>
		<view class="fg"></view>
		<view class="my-info bg-white">
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/user/mymatch"><image class="userinfo-img" src="/static/tennis/dawangqiu.png"></image><label class="userinfo-title">我的赛事</label></navigator>
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/user/mysoci"><image class="userinfo-img" src="/static/logo.png"></image><label class="userinfo-title">我的俱乐部/协会</label></navigator>
		</view>
		<view class="fg"></view>
		<view class="my-create bg-white">
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/match/addPerMatch"><image class="userinfo-img" src="/static/tennis/4.png"></image><label class="userinfo-title">发布个人赛</label></navigator>
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/match/addTeamMatch"><image class="userinfo-img" src="/static/tennis/tuandui.png" ></image><label class="userinfo-title">发布团体赛</label></navigator>
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/club_soci/create_cs?type=1"><image class="userinfo-img" src="/static/tennis/julebu-3.png"></image><label class="userinfo-title">创建俱乐部</label></navigator>
			<navigator hover-class="none" class="flex userinfo-nav" url="/pages/tennis/club_soci/create_cs?type=2"><image class="userinfo-img" src="/static/logo.png"></image><label class="userinfo-title">创建协会</label></navigator>
		</view>
		<view class="fg"></view>
		<view class="my-about bg-white">
			<navigator hover-class="none" class="flex userinfo-nav"><image class="userinfo-img" src="/static/tennis/yijianfankui.png"></image><label class="userinfo-title">意见反馈</label></navigator>
			<navigator hover-class="none" class="flex userinfo-nav"><image class="userinfo-img" src="/static/tennis/xinxi.png"></image><label class="userinfo-title">关于我们</label></navigator>
		</view>
		
	</view>
</template>

<script>
	export default {
		onShow() {
			this.userid=uni.getStorageSync("uid");
			this.openid=uni.getStorageSync("openid");
			
			if(this.userid.length<=0 || this.openid.length<=0){
				this.hasLogin=false;
			}else{
				this.hasLogin=true;
				uni.showLoading({
					title:'加载中..'
				})
				uni.request({
					url: 'https://www.52tennis.cn/User/GetUserInfo', //仅为示例，并非真实接口地址。
					data: {
						id:this.userid
					},
					success:(data)=> {
						if(data.data.result==1){
							let us= data.data.returnjson;
							this.headimg=us.headimg;
							this.nickname=us.nickname;
						}else{
							uni.showToast({
								title: '请求失败',
								icon: 'fail',
								mask: true,
								duration: 3000
							})
						}
					},
					complete: (res) => {
						uni.hideLoading();	
					}
				})
			}
		},
		onLoad: (option) => {
			
		},
		data() {
			return {
				userid:'',
				openid:'',
				headimg:'',
				nickname:'',
				hasLogin:false
			}
		},
		methods: {
			wxgetInfo(e){
				let _this =this;
				_this.userlogin();
			},
			userlogin(){
				let _this = this;
				
				uni.showLoading({
					title:'登陆中..'
				});
				uni.login({
					provider: 'weixin',
					success: function(loginRes) {
						let code = loginRes.code;
						uni.request({
							url:'https://api.weixin.qq.com/sns/jscode2session?appid=wxa2da079e71c05c56&secret=df3f9479f5198e20fe177ea327a73951&js_code='+code,
							header:{
								'Content-Type':'application/X-WWW-FORM-URLENCODED'
							},
							method:'POST',
							success:(res)=>{
								_this.openid=res.data.openid;
								uni.setStorageSync("openid",_this.openid);
								uni.getUserInfo({
									provider: 'weixin',
									success: function(infoRes) {
										console.log(infoRes);
										_this.headimg = infoRes.userInfo.avatarUrl;
										_this.nickname= infoRes.userInfo.nickName;
										uni.request({
											url:"https://www.52tennis.cn/User/InsertUser",
											header:{
												'Content-Type': 'application/json;charset=UTF-8' //自定义请求头信息
											},
											method:'POST',
											data: {
												openid:_this.openid,
												headimg:_this.headimg,
												nickname:_this.nickname,
												sex:1
											},success:(data)=> {
												console.log(data);
												if(data.data.result==0){
													_this.userid = data.data.msg;
													uni.setStorageSync("uid",_this.userid);
													
													setTimeout(function(){
														uni.switchTab({
															url:"/pages/tennis/user/index"
														})
													},500)
													
												}else if(data.data.result==1){
													_this.userid = data.data.msg;
													uni.setStorageSync("uid",_this.userid);
													
													//uni.setStorageSync("uid",data.data.msg);
													setTimeout(function(){
														uni.switchTab({
															url:"/pages/tennis/user/index"
														})
													},500)
												}else{
													_this.userid = '';
													_this.openid='';
													_this.hasLogin=false;
													uni.setStorageSync("uid",'');
													uni.setStorageSync("openid",'');
												}
											},
											fail: (res) => {
												_this.userid = '';
												_this.openid='';
												_this.hasLogin=false;
												uni.setStorageSync("uid",'');
												uni.setStorageSync("openid",'');
											}
										})
										
										
									},fail:function(res){
										console.log(res);
									},
									complete: () => {
										uni.hideLoading()
									}
								});
								
								
							}
						})
					 },
				});
			},
			updateUserInfo(){
				
			},
			navtoInfo(e){
				uni.navigateTo({
					url:"/pages/tennis/user/userinfo?hi="+this.headimg+"&nn="+this.nickname
				})
			},
		}
	}
</script>

<style>
	.userinfo{
		padding:30rpx;
		background-color: white;
	}
	.userinfo-login{
		color:#0081FF;
		font-size: 30rpx;
		padding-left: 0rpx;
	}
	.userinfo-det{
		margin-left: 50rpx;
	}
	.userinfo-nickname{
		font-size: 42rpx;
		height: 50%;
	}
	.userinfo-headimg{
		width: 120rpx;
		height: 120rpx;
		border-radius: 15rpx;
	}
	.fg{
		border-bottom: 10rpx solid #EBEEF5;
		margin-top:10rpx;
	}
	.my-info{padding:0rpx 30rpx;}
	.userinfo-img{
		width:30rpx;
		height:30rpx;
		margin-top:25rpx
	}
	.my-create{padding:0rpx 30rpx;}
	.my-about{padding:0rpx 30rpx;}

	.userinfo-nav{
		height: 80rpx;
		line-height: 80rpx;
		border-bottom: 1rpx solid #d4d2d1;
	}
	.userinfo-nav:last-child{
		border-bottom: none;
	}
	.userinfo-title{
		margin-left: 30rpx;
		font-size: 28rpx;
	}
</style>
